<template>
  <el-dialog
    title="调用关系"
    width="500px"
    :close-on-click-modal="false"
    :visible.sync="params.visible"
  >
    <el-form v-model="formData">
      <el-row :gutter="20">
        <el-col :span="12">
          <label>子调用关系</label>
          <p>组件名称</p>
          <compTree
            :show-search="false"
            :comp-tree-height="300"
            :tree-data="formData.treeData1"
          />
        </el-col>
        <el-col :span="12">
          <label>父调用关系</label>
          <p>组件名称</p>
          <compTree
            :show-search="false"
            :comp-tree-height="300"
            :tree-data="formData.treeData2"
          />
        </el-col>
      </el-row>
    </el-form>
    <dialog-btn slot="footer" />
  </el-dialog>
</template>

<script>
import { getComponentScriptLst } from '@/api/core'
import { ParamArr, SQLTable } from '../../../public/js/paramarr'
import Global from '../../../public/js/global'
export default {
  name: 'InvokRelationPro',
  props: {
    params: {
      type: Object,
      default() {
        return {
          visible: false,
          id: '' // 组件id
        }
      }
    }
  },
  data() {
    return {
      formData: {
        treeData1: [],
        treeData2: []
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      getComponentScriptLst(new ParamArr())
        .then((vPar) => {
          const table = new SQLTable(vPar.getTable('k_component'))
          if (table.locate('ID', [this.params.id])) {
            const caption = table.getValueByName('Name')
            const nodeMap = new Map()
            let nodes = []
            let pnode = Global.getTreeviewPNode(nodeMap, nodes, '')
            let node = {}
            node['id'] = this.params.id
            node['pid'] = ''
            node['text'] = caption
            node['icon'] = 'zujian'
            pnode.push(node)
            nodeMap.set(this.is, node)
            this.isHasChild(table, this.params.id, [this.params.id], pnode, nodeMap, nodes)
            this.formData.treeData1 = nodes

            nodeMap.clear()
            nodes = []
            pnode = Global.getTreeviewPNode(nodeMap, nodes, '')
            node = {}
            node['id'] = this.params.id
            node['pid'] = ''
            node['text'] = caption
            node['icon'] = 'zujian'
            pnode.push(node)
            nodeMap.set(node['id'], node)
            this.isHasParent(table, this.params.id, [this.params.id], pnode, nodeMap, nodes)
            this.formData.treeData2 = nodes
          } else {
            const node = {}
            node['id'] = this.params.id
            node['pid'] = ''
            node['text'] = this.params.id
            node['icon'] = 'zujian'
            this.formData.treeData1 = [node]
            this.formData.treeData2 = [node]
          }
        })
        .catch(() => {})
    },
    isHasChild(table, id, ids, pnode, nodeMap, nodes) {
      let ms = ''
      if (table.locate('ID', [id])) ms = table.getBlobStrByName('Script')
      if (ms !== '' && ms !== undefined) {
        const lines = ms.split('\r\n')
        for (let i = 0; i < lines.length; i++) {
          const line = lines[i].trim()
          if (
            line !== '' &&
            (line.indexOf('main_') !== -1 || line.indexOf('import_') !== -1)
          ) {
            if (
              line.indexOf(Global.strFormat('function main_{0}', id)) === 0 ||
              line.indexOf(Global.strFormat('procedure main_{0}', id)) === 0
            ) {
              continue
            }
            table.first()
            while (!table.eof()) {
              if (
                line.indexOf(table.getValueByName('ID')) !== -1 &&
                ids.toString().indexOf(table.getValueByName('ID')) === -1
              ) {
                const node = {}
                node['id'] = table.getValueByName('ID')
                node['pid'] = id
                node['text'] = table.getValueByName('Name')
                node['icon'] = 'zujian'
                pnode = Global.getTreeviewPNode(nodeMap, nodes, node['pid'])
                pnode.push(node)
                nodeMap.set(table.getValueByName('ID'), node)
                ids.push(node['id'])
                this.isHasChild(
                  table,
                  node['id'],
                  [node['id']],
                  pnode,
                  nodeMap,
                  nodes
                )
              }
              table.next()
            }
          }
        }
      }
    },
    isHasParent(table, id, ids, pnode, nodeMap, nodes) {
      const rows = []
      table.first()
      while (!table.eof()) {
        if (
          table.getValueByName('Type') !== 0 &&
          table.getValueByName('ID') !== id
        ) {
          var ms = table.getBlobStrByName('Script')
          if (ms !== '' && ms !== undefined) {
            const lines = ms.split('/r/n')
            for (var i = 0; i < lines.length; i++) {
              const line = lines[i].trim()
              if (
                (line !== '' &&
                  (line.indexOf(Global.strFormat('main_{0}', id)) !== -1 ||
                    line.indexOf(Global.strFormat('import_{0}', id)) !== -1) &&
                  line.indexOf(id) !== -1) ||
                ids.toString().indexOf(table.getValueByName('ID')) === -1
              ) {
                var node = {}
                node['id'] = table.getValueByName('ID')
                node['pid'] = id
                node['Script'] = ms
                node['text'] = table.getValueByName('Name')
                node['icon'] = 'zujian'
                rows.push(node)
                ids.push(node['id'])
              }
            }
          }
        }
        table.next()
      }
      this.InvokPro(rows, id, pnode, nodeMap, nodes)
    },
    InvokPro(rows, id, pnode, nodeMap, nodes) {
      for (var i = 0; i < rows.length; i++) {
        var lines = rows[i]['Script']
        if (
          (lines.indexOf('main_' + id) !== -1 ||
            lines.indexOf('import_' + id) !== -1) &&
          rows[i]['id'] !== id
        ) {
          var node = rows[i]
          node['pid'] = id
          pnode = Global.getTreeviewPNode(nodeMap, nodes, node['pid'])
          pnode.push(node)
          nodeMap.set(node['id'], node)
          this.InvokPro(rows, node['id'])
        }
      }
    },
    confirm() {
      if (this.params.confirm !== undefined) {
        this.params.confirm()
      } else {
        this.$emit('confirm', '')
      }
    }
  }
}
</script>

<style scoped>
</style>
